package com.sxy.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxy.pojo.Brand;
import com.sxy.pojo.PageBean;
import com.sxy.service.BrandService;
import com.sxy.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {
    private BrandService brandService = new BrandServiceImpl();

    /**
     * 查询所有数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用service查询
        List<Brand> brands = brandService.selectAll();
        //转成JSON字符串   序列化
        String jsonString = JSON.toJSONString(brands);
        //写入数据,因为是中文所以需要设置消息头
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    /**
     * 添加新数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收品牌数据
        BufferedReader br = req.getReader();
        String params = br.readLine();

        //转为Brand对象
        Brand brand = JSON.parseObject(params, Brand.class);

        //调用service添加
        brandService.add(brand);

        //返回响应成功标识
        resp.setContentType("text/text");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write("success");
    }

    /**
     * 修改数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收数据
        BufferedReader br = req.getReader();
        String s = br.readLine();

        //转为Brand对象
        Brand brand = JSON.parseObject(s, Brand.class);

        //调用方法
        brandService.update(brand);

        //返回响应成功标识
        resp.setContentType("text/text");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write("true");
    }

    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收数据
        BufferedReader br = req.getReader();
        String s = br.readLine();

        //转为int[]
        int[] ids = JSON.parseObject(s, int[].class);

        //调用方法
        brandService.deleteByIds(ids);

        //返回响应成功标识
        resp.setContentType("text/text");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write("true");
    }

    /**
     * 分页查询
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收当前页码和每页展示条数
        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");

        //转换成int
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //调用service查询
        PageBean<Brand> pageBean = brandService.selectByPage(currentPage, pageSize);
        //转成JSON字符串   序列化
        String jsonString = JSON.toJSONString(pageBean);
        //写入数据,因为是中文所以需要设置消息头
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    /**
     * 分页条件查询
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收当前页码和每页展示条数
        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");
        //获取对应的查询条件对象
        BufferedReader br = req.getReader();
        String param = br.readLine();
        //转为brand对象
        Brand brand = JSON.parseObject(param, Brand.class);
        //转换成int
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //调用service查询
        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize, brand);
        //转成JSON字符串   序列化
        String jsonString = JSON.toJSONString(pageBean);
        //写入数据,因为是中文所以需要设置消息头
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }
}
